package mobi.yuanshen.controller;

import lombok.extern.slf4j.Slf4j;
import mobi.yuanshen.pojo.Emp;
import mobi.yuanshen.service.EmpService;
import mobi.yuanshen.utils.JwtUtils;
import mobi.yuanshen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    EmpService empService;

    @Autowired
    JwtUtils jwtUtils;

    @PostMapping
    public Result login (@RequestBody Emp emp) {
        log.info("员工登录:{}",emp);
        Emp e = empService.login(emp);

        if (e != null ) {
            Map<String,Object> claims = new HashMap<>();
            claims.put("username", e.getUsername());
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            String key = jwtUtils.generateJwt(claims);
            return Result.success(key);
        }
        return Result.error("NOT_LOGIN");
    }
}
